// run: $exec < input
#include <iostream>
#include <cstring>
#include <string>

int const opt[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
int const maxn = 1007;
char map[maxn][maxn];
int n, m;

bool inrange(int x, int y)
{
	return 1 <= x && x <= n && 1 <= y && y <= m;
}

int dfs(int x, int y, int d, int lim, std::string const & s)
{
	if (d == lim) return 1;
	int ret = 0;
	for (int t = 0; t < 4; t++) {
		int tx = x + opt[t][0];
		int ty = y + opt[t][1];
		if (inrange(tx, ty) && map[tx][ty] == s[d])
			ret += dfs(tx, ty, d + 1, lim, s);
	}
	return ret;
}

int main()
{
	std::ios::sync_with_stdio(false);
	int T; std::cin >> T;
	while (T--) {
		std::memset(map, 0, sizeof(map));
		std::cin >> n >> m;
		for (int i = 1; i <= n; i++)
			for (int j = 1; j <= m; j++) std::cin >> map[i][j];
		int ans = 0;
		for (int i = 1; i <= n; i++)
			for (int j = 1; j <= m; j++) {
				if (map[i][j] == 'g') {
					int tx = i, ty = j;
					ans += dfs(tx, ty, 1, 4, "girl");
				}
			}
		std::cout << ans;
		ans = 0;
		for (int i = 1; i <= n; i++)
			for (int j = 1; j <= m; j++) {
				if (map[i][j] == 'c') {
					int tx = i, ty = j;
					ans += dfs(tx, ty, 1, 3, "cat");
				}
			}
		std::cout << " " << ans << "\n";
	}
}

